<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class EmailController extends Controller
{
	//权限管理首页
    public function getIndex(Request $request)
    {
         if($request['name']){
            //搜索分页
            $res = DB::table('email')
            ->join('person','person.PersonName','=','email.getName')
            ->where('email.sendName','like','%'.$request['name'].'%')
            ->where('email.getName',session('username'))
            ->orderby('email.status','asc')
            ->select('email.id','email.status','email.sendName','email.getName','email.title','email.file','email.email')
            ->paginate(3);

        }else{
            //所有分页
            $res = DB::table('email')
            ->join('person','person.PersonName','=','email.getName')
            ->where('email.getName',session('username'))
            ->orderby('email.status','asc')
            ->select('email.id','email.status','email.sendName','email.getName','email.title','email.file','email.email')
            ->paginate(3);
        }
       // dd($res);
       $info = $request -> all();
       $name = session('username');
       $num = DB::table('email')->where('getName',$name)->where('status',0)->count();
       // dd($num);
        return view('/email/email',['res'=>$res,'info'=>$info,'num'=>$num]);
    }

    //添加
    public function getAdd()
    {
        return view('/email/add');

    }

    //执行添加
    public function postInsert(Request $request)
    {
        if ($request->hasFile('file')) {
            //获取后缀名
            $ext = $request->file('file')->getClientOriginalExtension();
            // dd($ext);
            //检测后缀名
            $arr = array('jpg','png','jpeg','txt','doc','docx','xlsx');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                        alert("上传文件不合法");
                        location.href="http://lamp.cn/email/add";
                    </script>';
            }
            $fileName = time().rand(11111,99999).'.'.$ext;
            // dd($fileName);
            $request->file('file')->move('./uploads/', $fileName);
            $path = '/uploads/'.$fileName;
            // dd($path);
        }
        //表单验证
        $this->validate($request, [
        'getName' => 'required|exists:person,PersonName',
        'title' => 'required',
        'email' => 'required',
        ],[
        'getName.required'=>'收件人不能为空',
        'getName.exists'=>'收件人不存在',
        'title.required'=>'标题不能为空',
        'email.required'=>'内容不能为空',
        ]);
        $res = $request -> only('getName','title','email');
        if($request->hasFile('file')){

            $res['file'] = $path;
        }
        $res['sendName'] = session('username');
        $res['time'] = date('Y-m-d H:i:s',time());
        $res['status'] = 0;
        // dd($res);
        //执行添加
        DB::table('email')->insert($res);
        
        return redirect('/email');
        
    }
    //下载
    public function getUploads(Request $request){
       
        $text = $request->path();
        $text = substr($text,'6');
        $text1 = substr($text,'8');
        // dd($text);
        return response()->download($text, $text1);
    }


    //查看
    public function getLook($id)
    {
        DB::table('email')->where('id',$id)->update(['status'=>1]);
        $res = DB::table('email')->where('id',$id)->get();
        // dd($res);
        return view('/email/look',['res'=>$res]);
    }

    //删除
    public function getDel($id)
    {
        DB::table('email')->where('id',$id)->delete();
        return redirect('/email');
    }
}
?>